package com.shangke.corejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcDemo {//数据库连接包
	private static void jdbcSelect() {//对其进行封装
		//驱动
		String driver = "oracle.jdbc.driver.OracleDriver";
		//URL
		String url = "jdbc:oracle:thin://localhost:1521/orcl";
		//账号
		String user="student";
		//密码
		String password = "123456";
		
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try{
			//1.注册驱动
			Class.forName(driver);
			//2.连接数据库
			conn=DriverManager.getConnection(url,user,password);
			//3.打开状态连接
			stmt=conn.createStatement();
			//4.执行sql
			String sql="select * from cla";
			rs=stmt.executeQuery(sql);
			
			//String sql="delete from cla where id=1";
			//5.处理sql执行结果
			while (rs.next()) {
				int clano = rs.getInt(1);
				String claid=rs.getString(2);
				String claname=rs.getString(3);
				System.out.println(clano+"："+claid+"："+claname);
			}
		//6.关闭连接，释放资源
			//关闭的时候先关闭小的，与打开时相反
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					
				}
			}if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					
				}
			}if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}

	public static void main(String[] args) {
		jdbcSelect();

	}

}
